package gui;

import cards.HandLikelihood;
import cards.card;
import cards.deck;
import cards.hand;
import game.theGame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;

public class runGame extends Frame
    implements Runnable
{
    public class gui13mouse
        implements MouseListener
    {

        public void mouseClicked(MouseEvent e)
        {
            int x = e.getX();
            int y = e.getY();
            if(about)
            {
                about = false;
                return;
            }
            if(!winner)
            {
                if(x > 50 && x < 90 && y > 500 && y < 560)
                {
                    runGame.c1 = !runGame.c1;
                } else
                if(x > 100 && x < 140 && y > 500 && y < 560)
                {
                    runGame.c2 = !runGame.c2;
                } else
                if(x > 150 && x < 190 && y > 500 && y < 560)
                {
                    runGame.c3 = !runGame.c3;
                } else
                if(x > 200 && x < 240 && y > 500 && y < 560)
                {
                    runGame.c4 = !runGame.c4;
                } else
                if(x > 250 && x < 290 && y > 500 && y < 560)
                {
                    runGame.c5 = !runGame.c5;
                } else
                if(x > 300 && x < 340 && y > 500 && y < 560)
                {
                    runGame.c6 = !runGame.c6;
                } else
                if(x > 350 && x < 390 && y > 500 && y < 560)
                {
                    runGame.c7 = !runGame.c7;
                } else
                if(x > 400 && x < 440 && y > 500 && y < 560)
                {
                    runGame.c8 = !runGame.c8;
                } else
                if(x > 450 && x < 490 && y > 500 && y < 560)
                {
                    runGame.c9 = !runGame.c9;
                } else
                if(x > 500 && x < 540 && y > 500 && y < 560)
                {
                    runGame.c10 = !runGame.c10;
                } else
                if(x > 550 && x < 590 && y > 500 && y < 560)
                {
                    runGame.c11 = !runGame.c11;
                } else
                if(x > 600 && x < 640 && y > 500 && y < 560)
                {
                    runGame.c12 = !runGame.c12;
                } else
                if(x > 650 && x < 690 && y > 500 && y < 560)
                {
                    runGame.c13 = !runGame.c13;
                } else
                if(x > 50 && x < 750 && y > 170 && y < 340)
                {
                    submitCards();
                    runGame.richpoorselect = true;
                    tradecards = submitRP();
                    clearClicks();
                }
            } else
            {
                if(x > 370 && x < 445 && y > 350 && y < 375)
                {
                    playagain = false;
                    pchoice = true;
                }
                if(x > 250 && x < 325 && y > 350 && y < 375)
                {
                    playagain = true;
                    pchoice = true;
                }
            }
            repaint();
        }

        void clearClicks()
        {
            runGame.c1 = false;
            runGame.c2 = false;
            runGame.c3 = false;
            runGame.c4 = false;
            runGame.c5 = false;
            runGame.c6 = false;
            runGame.c7 = false;
            runGame.c8 = false;
            runGame.c9 = false;
            runGame.c10 = false;
            runGame.c11 = false;
            runGame.c12 = false;
            runGame.c13 = false;
            selection = false;
        }

        void submitCards()
        {
            s = "";
            if(runGame.c1)
            {
                selection = true;
                card theCard = (card)playershand.get(0);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c2)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(1);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c3)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(2);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c4)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(3);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c5)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(4);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c6)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(5);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c7)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(6);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c8)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(7);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c9)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(8);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c10)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(9);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c11)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(10);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c12)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(11);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            if(runGame.c13)
            {
                if(selection)
                {
                    s += 44;
                } else
                {
                    selection = true;
                }
                card theCard = (card)playershand.get(12);
                s += theCard.getSuit().charAt(0);
                s += theCard.getValue();
            }
            submit = true;
        }

        public void mouseReleased(MouseEvent mouseevent)
        {
        }

        public void mouseEntered(MouseEvent mouseevent)
        {
        }

        public void mousePressed(MouseEvent mouseevent)
        {
        }

        public void mouseExited(MouseEvent mouseevent)
        {
        }

        public gui13mouse()
        {
        }
    }


    private static BufferedReader stdin;
    hand h0;
    hand h1;
    hand h2;
    hand h3;
    String s;
    public int gamecount;
    public int wins0;
    public int wins1;
    public int wins2;
    public int wins3;
    boolean richpoor;
    boolean poorrich;
    boolean roundPassRule;
    boolean pass0;
    boolean pass1;
    boolean pass2;
    boolean pass3;
    static boolean richpoorselect = false;
    java.util.List tradecards;
    int lastwinner;
    int lastloser;
    theGame game;
    boolean cont;
    boolean winner;
    boolean playagain;
    boolean pchoice;
    int currentplayer;
    static boolean c1 = false;
    static boolean c2 = false;
    static boolean c3 = false;
    static boolean c4 = false;
    static boolean c5 = false;
    static boolean c6 = false;
    static boolean c7 = false;
    static boolean c8 = false;
    static boolean c9 = false;
    static boolean c10 = false;
    static boolean c11 = false;
    static boolean c12 = false;
    static boolean c13 = false;
    boolean selection;
    boolean submit;
    static Image cardImages;
    java.util.List playershand;
    java.util.List activehand;
    Thread gameThread;
    Graphics2D big;
    BufferedImage bi;
    boolean firsttime;
    boolean about;

    void resetPass()
    {
        pass0 = false;
        pass1 = false;
        pass2 = false;
        pass3 = false;
    }

    void resetScores()
    {
        theGame _tmp = game;
        theGame.score0 = 0;
        theGame _tmp1 = game;
        theGame.score1 = 0;
        theGame _tmp2 = game;
        theGame.score2 = 0;
        theGame _tmp3 = game;
        theGame.score3 = 0;
    }

    runGame()
    {
        gamecount = 1;
        lastwinner = -1;
        lastloser = -1;
        winner = false;
        playagain = true;
        pchoice = false;
        selection = false;
        playershand = new ArrayList();
        activehand = null;
        gameThread = null;
        firsttime = true;
        bi = new BufferedImage(800, 600, 1);
        big = bi.createGraphics();
        setBounds(0, 0, 800, 600);
        setTitle("Thirteen v.021");
        setVisible(true);
        cardImages = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/smallcards.gif"));
        addMouseListener(new gui13mouse());
        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e)
            {
                dispose();
                System.exit(0);
            }

        });
        deck d = new deck();
        d.shuffleDeck();
        h0 = new hand(d.getHand(13), 0);
        playershand = h0.theHand;
        h1 = new hand(d.getHand(13), 1);
        activehand = new ArrayList();
        h2 = new hand(d.getHand(13), 2);
        h3 = new hand(d.getHand(13), 3);
        game = new theGame(h0, h1, h2, h3, 0);
        game.threeOfSpades = false;
        gameThread = new Thread(this);
        gameThread.start();
    }

    public void run()
    {
        while(playagain) 
        {
            while(!h0.endGame() && !h1.endGame() && !h2.endGame() && !h3.endGame()) 
            {
                cont = true;
                if(h0.hasThree() && !game.threeOfSpades)
                {
                    game.threeOfSpades = true;
                    game.control = 0;
                }
                game.medianCardValue();
                while(cont && game.threeOfSpades && !pass0) 
                {
                    currentplayer = 0;
                    repaint();
                    if(game.control == 0)
                    {
                        resetPass();
                    }
                    for(submit = false; !submit;)
                    {
                        try
                        {
                            Thread.sleep(100L);
                            repaint();
                        }
                        catch(Exception e)
                        {
                            System.out.println("Error in running thread " + e);
                        }
                    }

                    String theInput = s;
                    if(theInput.equals(""))
                    {
                        if(game.control != 0)
                        {
                            cont = false;
                            pass0 = true;
                        }
                    } else
                    {
                        cont = !game.acceptor(game.inputToHand(theInput), 0);
                    }
                }
                if(!h0.endGame() && !h1.endGame() && !h2.endGame() && !h3.endGame())
                {
                    cont = true;
                }
                if(h1.hasThree() && !game.threeOfSpades)
                {
                    game.threeOfSpades = true;
                    game.control = 1;
                }
                while(cont && game.threeOfSpades && !pass1) 
                {
                    currentplayer = 1;
                    if(game.control == 1)
                    {
                        resetPass();
                        java.util.List a = HandLikelihood.controlLikely(h1, game);
                        if(a == null || a.isEmpty())
                        {
                            a = new ArrayList();
                            a.add(h1.theHand.get(0));
                        }
                        cont = !game.acceptor(a, 1);
                    } else
                    {
                        java.util.List hl = HandLikelihood.handlikely(h1, game);
                        if(hl == null || hl.isEmpty())
                        {
                            pass1 = true;
                            cont = false;
                        } else
                        {
                            cont = !game.acceptor(hl, 1);
                        }
                    }
                }
                turnPause();
                if(!h0.endGame() && !h1.endGame() && !h2.endGame() && !h3.endGame())
                {
                    cont = true;
                }
                if(h2.hasThree() && !game.threeOfSpades)
                {
                    game.threeOfSpades = true;
                    game.control = 2;
                }
                while(cont && game.threeOfSpades && !pass2) 
                {
                    currentplayer = 2;
                    if(game.control == 2)
                    {
                        resetPass();
                        java.util.List a = HandLikelihood.controlLikely(h2, game);
                        if(a == null || a.isEmpty())
                        {
                            a = new ArrayList();
                            a.add(h2.theHand.get(0));
                        }
                        cont = !game.acceptor(a, 2);
                    } else
                    {
                        java.util.List hl = HandLikelihood.handlikely(h2, game);
                        if(hl == null || hl.isEmpty())
                        {
                            cont = false;
                            pass2 = true;
                        } else
                        {
                            cont = !game.acceptor(hl, 2);
                        }
                    }
                }
                turnPause();
                if(!h0.endGame() && !h1.endGame() && !h2.endGame() && !h3.endGame())
                {
                    cont = true;
                }
                if(h3.hasThree() && !game.threeOfSpades)
                {
                    game.threeOfSpades = true;
                    game.control = 3;
                }
                while(cont && game.threeOfSpades && !pass3) 
                {
                    currentplayer = 3;
                    if(game.control == 3)
                    {
                        resetPass();
                        java.util.List a = HandLikelihood.controlLikely(h3, game);
                        if(a == null || a.isEmpty())
                        {
                            a = new ArrayList();
                            a.add(h3.theHand.get(0));
                        }
                        cont = !game.acceptor(a, 3);
                    } else
                    {
                        java.util.List hl = HandLikelihood.handlikely(h3, game);
                        if(hl == null || hl.isEmpty())
                        {
                            pass3 = true;
                            cont = false;
                        } else
                        {
                            cont = !game.acceptor(hl, 3);
                        }
                    }
                }
                turnPause();
                repaint();
                try
                {
                    Thread.sleep(100L);
                }
                catch(Exception e)
                {
                    System.out.println("Error in running thread " + e);
                }
            }
            winner = true;
            if(h0.endGame())
            {
                wins0++;
                lastwinner = 0;
                theGame.score0 += h1.theHand.size() + h2.theHand.size() + h3.theHand.size();
            } else
            if(h1.endGame())
            {
                lastwinner = 1;
                wins1++;
                theGame.score1 += h0.theHand.size() + h2.theHand.size() + h3.theHand.size();
            } else
            if(h2.endGame())
            {
                lastwinner = 2;
                wins2++;
                theGame.score2 += h1.theHand.size() + h0.theHand.size() + h3.theHand.size();
            } else
            if(h3.endGame())
            {
                lastwinner = 3;
                wins3++;
                theGame.score3 += h1.theHand.size() + h2.theHand.size() + h0.theHand.size();
            }
            repaint();
            for(pchoice = false; !pchoice;) { }
            if(!playagain)
            {
                dispose();
                System.exit(0);
            }
            winner = false;
            getLastLost();
            deck d = new deck();
            d.shuffleDeck();
            h0 = new hand(d.getHand(13), 0);
            playershand = h0.theHand;
            h1 = new hand(d.getHand(13), 1);
            activehand = new ArrayList();
            h2 = new hand(d.getHand(13), 2);
            h3 = new hand(d.getHand(13), 3);
            if(poorrich)
            {
                int temp = lastwinner;
                lastwinner = lastloser;
                lastloser = temp;
            }
            if(richpoor || poorrich)
            {
                switch(lastwinner)
                {
                case 0: // '\0'
                    rpChoice();
                    break;

                case 1: // '\001'
                    h1 = cpuRman(h1);
                    break;

                case 2: // '\002'
                    h2 = cpuRman(h2);
                    break;

                case 3: // '\003'
                    h3 = cpuRman(h3);
                    break;
                }
            }
            game = new theGame(h0, h1, h2, h3, 0);
            game.threeOfSpades = false;
        }
    }

    void getLastLost()
    {
        if(h0.theHand.size() >= h1.theHand.size() && h0.theHand.size() >= h2.theHand.size() && h0.theHand.size() >= h3.theHand.size())
        {
            lastloser = 0;
        } else
        if(h1.theHand.size() >= h2.theHand.size() && h1.theHand.size() >= h3.theHand.size() && h1.theHand.size() >= h0.theHand.size())
        {
            lastloser = 1;
        } else
        if(h2.theHand.size() >= h1.theHand.size() && h2.theHand.size() >= h3.theHand.size() && h2.theHand.size() >= h0.theHand.size())
        {
            lastloser = 2;
        } else
        if(h3.theHand.size() >= h1.theHand.size() && h3.theHand.size() >= h2.theHand.size() && h3.theHand.size() >= h0.theHand.size())
        {
            lastloser = 3;
        }
    }

    hand cpuRman(hand trade)
    {
        repaint();
        try
        {
            Thread.sleep(3000L);
            repaint();
        }
        catch(Exception exception) { }
        java.util.List trading = new ArrayList();
        if(trade.freeSingles().size() > 2)
        {
            if(((card)trade.freeSingles().get(0)).getValue() < 9)
            {
                trading.add(trade.freeSingles().get(0));
            }
            if(((card)trade.freeSingles().get(1)).getValue() < 9)
            {
                trading.add(trade.freeSingles().get(1));
            }
        }
        if(trading.size() < 2)
        {
            trading.add(trade.theHand.get(0));
        }
        if(trading.size() < 2)
        {
            trading.add(trade.theHand.get(1));
        }
        switch(lastloser)
        {
        case 0: // '\0'
        {
            java.util.List getTrade = h0.twoAtRandom();
            h0.delHandFromHand(getTrade);
            h0.addHandToHand(trading);
            h0 = new hand(h0.theHand, 0);
            trade.delHandFromHand(trading);
            trade.addHandToHand(getTrade);
            break;
        }

        case 1: // '\001'
        {
            java.util.List getTrade = h1.twoAtRandom();
            h1.delHandFromHand(getTrade);
            h1.addHandToHand(trading);
            h1 = new hand(h1.theHand, 1);
            trade.delHandFromHand(trading);
            trade.addHandToHand(getTrade);
            break;
        }

        case 2: // '\002'
        {
            java.util.List getTrade = h2.twoAtRandom();
            h2.delHandFromHand(getTrade);
            h2.addHandToHand(trading);
            h2 = new hand(h2.theHand, 2);
            trade.delHandFromHand(trading);
            trade.addHandToHand(getTrade);
            break;
        }

        case 3: // '\003'
        {
            java.util.List getTrade = h3.twoAtRandom();
            h3.delHandFromHand(getTrade);
            h3.addHandToHand(trading);
            h3 = new hand(h3.theHand, 3);
            trade.delHandFromHand(trading);
            trade.addHandToHand(getTrade);
            break;
        }
        }
        trade = new hand(trade.theHand, lastwinner);
        repaint();
        try
        {
            Thread.sleep(1000L);
            repaint();
        }
        catch(Exception exception1) { }
        return trade;
    }

    void rpChoice()
    {
        for(richpoorselect = false; !richpoorselect;)
        {
            try
            {
                Thread.sleep(100L);
                repaint();
            }
            catch(Exception e)
            {
                System.out.println("Error in running thread " + e);
            }
        }

        if(tradecards.size() != 2)
        {
            rpChoice();
        } else
        {
            switch(lastloser)
            {
            case 1: // '\001'
            {
                java.util.List getTrade = h1.twoAtRandom();
                h1.delHandFromHand(getTrade);
                h1.addHandToHand(tradecards);
                h0.delHandFromHand(tradecards);
                h0.addHandToHand(getTrade);
                h1 = new hand(h1.theHand, 1);
                h0 = new hand(h0.theHand, 0);
                break;
            }

            case 2: // '\002'
            {
                java.util.List getTrade = h2.twoAtRandom();
                h2.delHandFromHand(getTrade);
                h2.addHandToHand(tradecards);
                h0.delHandFromHand(tradecards);
                h0.addHandToHand(getTrade);
                h2 = new hand(h2.theHand, 2);
                h0 = new hand(h0.theHand, 0);
                break;
            }

            case 3: // '\003'
            {
                java.util.List getTrade = h3.twoAtRandom();
                h3.delHandFromHand(getTrade);
                h3.addHandToHand(tradecards);
                h0.delHandFromHand(tradecards);
                h0.addHandToHand(getTrade);
                h3 = new hand(h3.theHand, 3);
                h0 = new hand(h0.theHand, 0);
                break;
            }
            }
        }
    }

    java.util.List submitRP()
    {
        java.util.List s = new ArrayList();
        if(c1)
        {
            card theCard = (card)h0.theHand.get(0);
            s.add(theCard);
        }
        if(c2)
        {
            card theCard = (card)h0.theHand.get(1);
            s.add(theCard);
        }
        if(c3)
        {
            card theCard = (card)h0.theHand.get(2);
            s.add(theCard);
        }
        if(c4)
        {
            card theCard = (card)h0.theHand.get(3);
            s.add(theCard);
        }
        if(c5)
        {
            card theCard = (card)h0.theHand.get(4);
            s.add(theCard);
        }
        if(c6)
        {
            card theCard = (card)h0.theHand.get(5);
            s.add(theCard);
        }
        if(c7)
        {
            card theCard = (card)h0.theHand.get(6);
            s.add(theCard);
        }
        if(c8)
        {
            card theCard = (card)h0.theHand.get(7);
            s.add(theCard);
        }
        if(c9)
        {
            card theCard = (card)h0.theHand.get(8);
            s.add(theCard);
        }
        if(c10)
        {
            card theCard = (card)h0.theHand.get(9);
            s.add(theCard);
        }
        if(c11)
        {
            card theCard = (card)h0.theHand.get(10);
            s.add(theCard);
        }
        if(c12)
        {
            card theCard = (card)h0.theHand.get(11);
            s.add(theCard);
        }
        if(c13)
        {
            card theCard = (card)h0.theHand.get(12);
            s.add(theCard);
        }
        return s;
    }

    void turnPause()
    {
        activehand = game.activeHand;
        repaint();
        try
        {
            Thread.sleep(1000L);
            repaint();
        }
        catch(Exception exception) { }
    }

    public void paint(Graphics g)
    {
        update(g);
    }

    public void update(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        big.setColor(new Color(126, 126, 243));
        big.fillRect(0, 0, 800, 600);
        big.setColor(Color.black);
        big.setColor(new Color(0, 145, 72));
        big.fillRect(0, 400, 800, 300);
        big.setColor(new Color(179, 122, 0));
        big.fillRect(0, 550, 800, 50);
        big.setColor(Color.black);
        big.fillRect(0, 400, 800, 2);
        big.fillRect(0, 550, 800, 2);
        big.setColor(Color.black);
        big.fillRect(0, 380, 800, 1);
        big.setColor(Color.orange);
        big.fillRect(0, 381, 800, 1);
        big.setColor(new Color(179, 122, 0));
        big.fillRect(0, 382, 800, 19);
        big.setColor(Color.white);
        big.drawString("Your Hand:", 100, 480);
        Iterator iter = playershand.iterator();
        for(int i = 0; iter.hasNext(); i++)
        {
            card c = (card)iter.next();
            drawCard(big, c.getValue(), c.getSuit(), 50 * (i + 1), 500);
        }

        big.setColor(new Color(179, 122, 0));
        big.fillRoundRect(50, 170, 700, 170, 10, 10);
        big.setColor(new Color(0, 145, 72));
        big.fillRoundRect(75, 180, 650, 150, 10, 10);
        big.setColor(Color.black);
        big.drawRoundRect(75, 180, 650, 150, 10, 10);
        big.setColor(Color.black);
        big.drawRoundRect(50, 170, 700, 170, 10, 10);
        big.setColor(Color.orange);
        big.drawRoundRect(51, 171, 698, 168, 10, 10);
        big.setColor(Color.black);
        big.drawRoundRect(76, 181, 648, 148, 10, 10);
        if(activehand != null)
        {
            iter = activehand.iterator();
            int i = 0;
            card c;
            for(; iter.hasNext(); drawCard(big, c.getValue(), c.getSuit(), 50 * i + (400 - activehand.size() * 40), 230))
            {
                c = (card)iter.next();
                i++;
            }

            big.setColor(Color.black);
        } else
        {
            for(int j = 0; j < 13; j++)
            {
                drawCard(big, -1, "Spades", 47 * j + 100, 230);
            }

        }
        paintHandState(big);
        controlMarker(big);
        turnMarker(big);
        drawOtherHands(big);
        drawScoreBoard(big);
        if(this.winner)
        {
            big.setColor(Color.black);
            big.drawRect(200, 200, 300, 250);
            big.setColor(new Color(99, 99, 237, 90));
            big.fillRect(200, 200, 300, 250);
            String winner = "PLAYER ";
            if(h0.endGame())
            {
                winner = winner + "0";
            } else
            if(h1.endGame())
            {
                winner = winner + "1";
            } else
            if(h2.endGame())
            {
                winner = winner + "2";
            } else
            if(h3.endGame())
            {
                winner = winner + "3";
            }
            winner = winner + " WINS THE GAME";
            big.setColor(Color.black);
            big.drawString(winner, 280, 220);
            big.setColor(Color.black);
            big.setFont(new Font("Arial", 1, 30));
            big.drawString("Play Again?", 270, 300);
            big.setColor(Color.black);
            big.drawRect(250, 350, 75, 25);
            big.setColor(new Color(99, 99, 237));
            big.fillRect(250, 350, 75, 25);
            big.setColor(Color.white);
            big.setFont(new Font("Arial", 0, 12));
            big.drawString("YES", 278, 367);
            big.setColor(Color.black);
            big.drawRect(370, 350, 75, 25);
            big.setColor(new Color(99, 99, 237));
            big.fillRect(370, 350, 75, 25);
            big.setColor(Color.white);
            big.setFont(new Font("Arial", 0, 12));
            big.drawString("NO", 398, 367);
        }
        if(about)
        {
            paintAbout(big);
        }
        g2.drawImage(bi, 0, 0, this);
    }

    void paintAbout(Graphics g)
    {
        g.setColor(Color.darkGray);
        g.fillRect(295, 195, 310, 125);
        g.setColor(Color.black);
        g.drawRect(295, 195, 310, 125);
        g.setColor(Color.white);
        g.fillRect(300, 200, 270, 95);
        g.setColor(Color.black);
        g.drawRect(300, 200, 270, 95);
        g.setColor(Color.gray);
        g.drawRect(302, 202, 270, 95);
        g.setColor(Color.black);
        g.setFont(new Font("Comic Sans MS", 1, 15));
        g.drawString("Thirteen v.021", 330, 220);
        g.setFont(new Font("Comic Sans MS", 0, 10));
        g.drawString("By Brandon Wong", 330, 235);
        g.drawString("brandon@abstractpixel.com", 330, 250);
        g.drawString("http://abstractpixel.com/bw/thirteen", 330, 265);
        g.setColor(Color.GRAY);
        g.drawString("[Click to Close]", 515, 312);
        g.drawRect(300, 305, 10, 10);
        g.drawRect(591, 200, 10, 10);
        g.setFont(new Font("Comic Sans MS", 1, 12));
    }

    void drawScoreBoard(Graphics g)
    {
        g.setColor(Color.orange);
        g.fillRect(410, 370, 270, 95);
        g.setColor(Color.black);
        g.drawRect(410, 370, 270, 95);
        g.setColor(new Color(0, 145, 72));
        g.fillRect(420, 380, 250, 75);
        g.setColor(Color.white);
        g.drawRect(420, 380, 250, 75);
        g.setFont(new Font("Comic Sans MS", 1, 12));
        g.drawString(" w ", 550, 390);
        g.drawString("P0 Score: " + theGame.score0, 450, 403);
        g.drawString("P1 Score: " + theGame.score1, 450, 418);
        g.drawString("P2 Score: " + theGame.score2, 450, 433);
        g.drawString("P3 Score: " + theGame.score3, 450, 448);
        g.drawString(" " + wins0, 550, 403);
        g.drawString(" " + wins1, 550, 418);
        g.drawString(" " + wins2, 550, 433);
        g.drawString(" " + wins3, 550, 448);
        int totalgames = wins0 + wins1 + wins2 + wins3;
        g.setFont(new Font("Comic Sans MS", 1, 30));
        g.drawString((new StringBuffer(String.valueOf(totalgames))).toString(), 606, 435);
        g.setFont(new Font("Comic Sans MS", 1, 12));
        g.drawString("game no.", 600, 403);
    }

    void turnMarker(Graphics g)
    {
        g.setColor(Color.red);
        if(currentplayer == 0)
        {
            g.fillOval(235, 450, 20, 20);
            g.setColor(Color.black);
            g.drawOval(235, 450, 20, 20);
            g.setColor(Color.black);
            g.drawOval(236, 451, 18, 18);
        }
        if(currentplayer == 1)
        {
            g.fillOval(185, 60, 20, 20);
            g.setColor(Color.black);
            g.drawOval(185, 60, 20, 20);
            g.setColor(Color.black);
            g.drawOval(186, 60, 18, 18);
        }
        if(currentplayer == 2)
        {
            g.fillOval(405, 60, 20, 20);
            g.setColor(Color.black);
            g.drawOval(405, 60, 20, 20);
            g.setColor(Color.black);
            g.drawOval(406, 60, 18, 18);
        }
        if(currentplayer == 3)
        {
            g.fillOval(625, 60, 20, 20);
            g.setColor(Color.black);
            g.drawOval(625, 60, 20, 20);
            g.setColor(Color.black);
            g.drawOval(626, 60, 18, 18);
        }
    }

    void controlMarker(Graphics g)
    {
        if(game != null)
        {
            g.setColor(Color.blue);
            if(game.control == 0)
            {
                g.fillOval(210, 450, 20, 20);
                g.setColor(Color.black);
                g.drawOval(210, 450, 20, 20);
                g.setColor(Color.black);
                g.drawOval(211, 451, 18, 18);
            }
            if(game.control == 1)
            {
                g.fillOval(160, 60, 20, 20);
                g.setColor(Color.black);
                g.drawOval(160, 60, 20, 20);
                g.setColor(Color.black);
                g.drawOval(161, 60, 18, 18);
            }
            if(game.control == 2)
            {
                g.fillOval(380, 60, 20, 20);
                g.setColor(Color.black);
                g.drawOval(380, 60, 20, 20);
                g.setColor(Color.black);
                g.drawOval(381, 60, 18, 18);
            }
            if(game.control == 3)
            {
                g.fillOval(600, 60, 20, 20);
                g.setColor(Color.black);
                g.drawOval(600, 60, 20, 20);
                g.setColor(Color.black);
                g.drawOval(601, 60, 18, 18);
            }
        }
    }

    void paintHandState(Graphics g)
    {
        int i;
        if(h0 == null || h0.theHand == null)
        {
            i = 0;
        } else
        {
            i = h0.theHand.size();
        }
        switch(i)
        {
        case 1: // '\001'
            c2 = false;
            // fall through

        case 2: // '\002'
            c3 = false;
            // fall through

        case 3: // '\003'
            c4 = false;
            // fall through

        case 4: // '\004'
            c5 = false;
            // fall through

        case 5: // '\005'
            c6 = false;
            // fall through

        case 6: // '\006'
            c7 = false;
            // fall through

        case 7: // '\007'
            c8 = false;
            // fall through

        case 8: // '\b'
            c9 = false;
            // fall through

        case 9: // '\t'
            c10 = false;
            // fall through

        case 10: // '\n'
            c11 = false;
            // fall through

        case 11: // '\013'
            c12 = false;
            // fall through

        case 12: // '\f'
            c13 = false;
            break;
        }
        if(c1)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(49, 499, 41, 61);
        }
        if(c2)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(99, 499, 41, 61);
        }
        if(c3)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(149, 499, 41, 61);
        }
        if(c4)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(199, 499, 41, 61);
        }
        if(c5)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(249, 499, 41, 61);
        }
        if(c6)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(299, 499, 41, 61);
        }
        if(c7)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(349, 499, 41, 61);
        }
        if(c8)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(399, 499, 41, 61);
        }
        if(c9)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(449, 499, 41, 61);
        }
        if(c10)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(499, 499, 41, 61);
        }
        if(c11)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(549, 499, 41, 61);
        }
        if(c12)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(599, 499, 41, 61);
        }
        if(c13)
        {
            g.setColor(new Color(255, 255, 0));
            g.drawRect(649, 499, 41, 61);
        }
    }

    void drawCard(Graphics g, int value, String suit, int x, int y)
    {
        char sc = suit.charAt(0);
        int row = 0;
        switch(sc)
        {
        case 67: // 'C'
            row = 0;
            break;

        case 72: // 'H'
            row = 1;
            break;

        case 83: // 'S'
            row = 2;
            break;

        case 68: // 'D'
            row = 3;
            break;
        }
        int sx = 40 * ((value + 2) % 13);
        int sy = 60 * row;
        if(value != -1)
        {
            g.drawImage(cardImages, x, y, x + 40, y + 60, sx, sy, sx + 40, sy + 60, this);
        }
    }

    void drawOtherHands(Graphics g)
    {
        if(h0 != null)
        {
            int x = 50;
            int y = 100;
            g.setColor(Color.black);
            g.drawString("Player 1's Cards", x, y - 20);
            for(int i = 0; i < h1.theHand.size(); i++)
            {
                g.setColor(Color.white);
                g.fillRect(x + i * 11, y, 40, 60);
                g.setColor(Color.red);
                g.fillRect(x + 3 + i * 11, y + 3, 34, 54);
                g.setColor(Color.black);
                g.drawRect(x + i * 11, y, 40, 60);
            }

            x = 270;
            y = 100;
            g.setColor(Color.black);
            g.drawString("Player 2's Cards", x, y - 20);
            for(int i = 0; i < h2.theHand.size(); i++)
            {
                g.setColor(Color.white);
                g.fillRect(x + i * 11, y, 40, 60);
                g.setColor(Color.red);
                g.fillRect(x + 3 + i * 11, y + 3, 34, 54);
                g.setColor(Color.black);
                g.drawRect(x + i * 11, y, 40, 60);
            }

            x = 490;
            y = 100;
            g.setColor(Color.black);
            g.drawString("Player 3's Cards", x, y - 20);
            for(int i = 0; i < h3.theHand.size(); i++)
            {
                g.setColor(Color.white);
                g.fillRect(x + i * 11, y, 40, 60);
                g.setColor(Color.red);
                g.fillRect(x + 3 + i * 11, y + 3, 34, 54);
                g.setColor(Color.black);
                g.drawRect(x + i * 11, y, 40, 60);
            }

        }
    }

    static 
    {
        stdin = new BufferedReader(new InputStreamReader(System.in));
    }
}
